package com.hs.task.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by chenyq5 on 2016-4-7.
 */
public class ExcelFileUtil {

    public static Workbook readExcel(String fileName) throws IOException {
        InputStream input = null;
        try {
            boolean isE2007 = false;    //判断是否是excel2007格式
            if (fileName.endsWith("xlsx")) {
                isE2007 = true;
            }
            input = new FileInputStream(fileName);    //建立输入流
            Workbook wb = null;
            //根据文件格式(2003或者2007)来初始化
            if (isE2007) {
                wb = new XSSFWorkbook(input);
            } else {
                wb = new HSSFWorkbook(input);
            }
            return wb;
        } finally {
            if (input != null) {
                input.close();
            }
        }

    }

    public static String getResult(Sheet sheet, int startRow, int endRow, int col) {
        StringBuffer sb = new StringBuffer();
        for (int i = startRow; i < endRow; i++) {
            if (sheet.getRow(i) == null) {
                break;
            }
            sb.append(sheet.getRow(i).getCell(col) + "\n");
        }
        String str = sb.toString();
        if (str.length() > 0) {
            int index = str.lastIndexOf("\n");
            str = str.substring(0, index);
        }
        return str;
    }

    public static String getResult(Sheet sheet, int startRow, int col) { //获取开始行到文件结束的内容
        int endRow = startRow;
        while (sheet.getRow(endRow) != null) {
            endRow++;
        }
        return getResult(sheet, startRow, endRow, col);
    }

    public static Object getCellValue(Cell c) {
        Object obj = null;
        if (c == null) {
            return null;
        }
        switch (c.getCellType()) {
            case Cell.CELL_TYPE_STRING: {
                obj = c.getStringCellValue();
                break;
            }
            case Cell.CELL_TYPE_NUMERIC: {
                obj = c.getNumericCellValue();
                break;
            }
            case Cell.CELL_TYPE_BOOLEAN: {
                obj = c.getBooleanCellValue();
                break;
            }
            case Cell.CELL_TYPE_ERROR: {
                obj = c.getErrorCellValue();
            }
            default:
                break;
        }
        return obj;
    }


    public static void main(String[] argvs) {
        String filePath = "D:\\鹰眼数据\\160324_999999_00001\\160324_999999_00001.xlsx";
        try {
            Workbook wb = readExcel(filePath);
            Sheet sheet = wb.getSheetAt(0);
            int i = 132;
            int j = i;
            while (sheet.getRow(j) != null) {
                j++;
            }
            System.out.println(j);
            String result = getResult(sheet, i, j, 1);
            System.out.println(result);
            System.out.println("&&&&&&&&&&");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
